package com.lfc.myframe.utils

import android.app.Activity
import android.content.Context
import com.lfc.myframe.widget.WaitDialog
import java.lang.ref.WeakReference

/**
 * 控制一个页面不能有多个Dialog
 */
object LoadingUtil {
    private var mThreadActivityRef: WeakReference<Context>? = null
    private var waitDialog: WeakReference<WaitDialog?>? = null
    /**
     * 自定义用于等待的dialog,有动画和message提示
     * 调用stopWaitDialog()方法来取消
     *
     * @param activity
     * @param message
     */
    fun showLoading(activity: Activity?, message: String?) {
        if (waitDialog != null && waitDialog!!.get() != null && waitDialog!!.get()!!.isShowing) {
            waitDialog!!.get()!!.dismiss()
        }
        if (activity == null || activity.isFinishing) {
            return
        }
        mThreadActivityRef =
            WeakReference(activity)
        waitDialog = WeakReference(
            WaitDialog.setDialog(
                mThreadActivityRef!!.get(),
                message,
                null
            )
        )
        if (waitDialog != null && waitDialog!!.get() != null && !waitDialog!!.get()!!.isShowing) {
            waitDialog!!.get()!!.setCanceledOnTouchOutside(false)
            waitDialog!!.get()!!.setCancelable(false)
            waitDialog!!.get()!!.show() //
        }
    }

    /**
     * 取消等待dialog
     */
    fun dismissLoading() {
        if (waitDialog != null && waitDialog!!.get() != null && waitDialog!!.get()!!.isShowing) {
            waitDialog!!.get()!!.dismiss()
        }
    }
}